﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Core.Message.Entities;
using Core.ExceptionHandling;
using Core.Controls.Message;
using vHCS.HR.Business;
using vHCS.LS.Business;
using Telerik.Web.UI;
using vHCS.SYS.Entities;
using Core.Common.Session;
using vHCS.K2App.Shared;

namespace vHCS.K2App.MdlK2
{
    public partial class WordkingReocrdRequestHis : Core.NTier.UserInterface.BaseFormNotPer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            try
            {
                // Check user incharge to view history
                string userAccess = SessionManager.UserDomain;
                int Id = Convert.ToInt32(Request.QueryString["ID"].ToString());
                HistoryComment his = new HistoryComment();
                if (!his.CheckUserInCharge(userAccess, Id, 1)) // If user not in charge
                {
                    Response.Redirect("../UnAuthorize.htm");
                }

                if (!IsPostBack)
                {
                    hdfWRNow.Value = Request.QueryString["ID"] != null ? Request.QueryString["ID"].ToString() : "";
                    if (hdfWRNow.Value != "")
                    {
                        LoadWRForApprove();
                    }

                    dtsAllowance.SelectParameters["workingnow"].DefaultValue = hdfWRNow.Value;
                    dtsAllowance.SelectParameters["workingprevious"].DefaultValue = hdfWRPre.Value;
                    dtgTrocapkhac.DataSourceID = dtsAllowance.ID;
                    dtgTrocapkhac.DataBind();
                    ClientScript.RegisterStartupScript(Page.GetType(), "CheckDecision", "ChangeSalary();", true);
                }
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }


        #region Function
        /// <summary>
        /// Load thông tin luân chuyển nhân sự mới nhất của nhân viên
        /// </summary>
        public void LoadWRForApprove()
        {
            try
            {
                if (hdfWRNow.Value !="")
                {
                    HR_WorkingRecordBL bl = new HR_WorkingRecordBL();
                    HR_WorkingRecordCollection cl = new HR_WorkingRecordCollection();
                    bl = cl.SelectByID(Convert.ToInt64(hdfWRNow.Value));
                    if (bl.DTO != null)
                    {
                        HR_ctrEmpHeader.LoadEmpInfoByEmpId(bl.EmpID);
                        hdfWRNow.Value = bl.WorkingRecordID.ToString();

                        dpkNgayhieuluc.SelectedDate = bl.FromDate;

                        LS_ChangeFormBL bl_change = new LS_ChangeFormBL();
                        LS_ChangeFormCollection cl_change = new LS_ChangeFormCollection();
                        bl_change = cl_change.SelectByID(bl.LSStatusChangeID);
                        if (bl_change.DTO != null)
                            txtHinhthucthaydoi.Text = (SessionManager.LanguageID == "VN" || string.IsNullOrEmpty(bl_change.NameEN)) ? bl_change.NameVN : bl_change.NameEN;

                        if (bl.Decision == true)
                            rbtTerminationStatus.SelectedValue = "1";
                        else
                            rbtTerminationStatus.SelectedValue = "2";

                        txtFile.Text = bl.AttachFile;

                        LS_WorkPlaceBL bl_workplace = new LS_WorkPlaceBL();
                        LS_WorkPlaceCollection cl_workplace = new LS_WorkPlaceCollection();
                        bl_workplace = cl_workplace.SelectByID(bl.LSWorkPlaceID);
                        if (bl_workplace.DTO != null)
                            txtOffice.Text = (SessionManager.LanguageID == "VN" || string.IsNullOrEmpty(bl_workplace.NameEN)) ? bl_workplace.NameVN : bl_workplace.NameEN;

                        //Load cơ cấu tổ chức
                        txtOrgChart.Text = cl.SelectOrgChartName(bl.WorkingRecordID);

                        //Load cơ cấu chuyên môn
                        txtProChart.Text = cl.SelectProJobNameFull(bl.WorkingRecordID);

                        //Load cơ cấu quản lý
                        txtMgtChart.Text = cl.SelectMgtJobNameFull(bl.WorkingRecordID);

                        //Load cấp bậc
                        LS_JobLevelBL bl_joblevel = new LS_JobLevelBL();
                        LS_JobLevelCollection cl_joblevel = new LS_JobLevelCollection();
                        bl_joblevel = cl_joblevel.SelectByID(bl.LSLevelID);
                        if (bl_joblevel.DTO != null)
                            txtLevel.Text = (SessionManager.LanguageID == "VN" || bl_joblevel.NameEN == "") ? bl_joblevel.NameVN : bl_joblevel.NameEN;

                        //Load chức vụ
                        LS_JobTitleBL bl_title = new LS_JobTitleBL();
                        LS_JobTitleCollection cl_title = new LS_JobTitleCollection();
                        bl_title = cl_title.SelectByID(bl.JobTitleID);
                        if (bl_title.DTO != null)
                            txtJobTitle.Text = (SessionManager.LanguageID == "VN" || bl_title.NameEN == "") ? bl_title.NameVN : bl_title.NameEN;

                        //Load jobrole
                        txtJobRole.Text = cl.SelectJobRoleName(bl.WorkingRecordID);

                        //Load loại nhân viên
                        LS_EmpTypeBL bl_emptype = new LS_EmpTypeBL();
                        LS_EmpTypeCollection cl_emptype = new LS_EmpTypeCollection();
                        bl_emptype = cl_emptype.SelectByID(bl_joblevel.LSEmpTypeID);
                        if (bl_emptype.DTO != null)
                            txtEmpType.Text = (SessionManager.LanguageID == "VN" || bl_emptype.NameEN == "") ? bl_emptype.NameVN : bl_emptype.NameEN;

                        txtLeader.Text = cl.ReturnLeader(bl.HierarchyId);

                        HR_EmpCVCollection cl_empcv = new HR_EmpCVCollection();
                        txtReporter.Text = cl_empcv.GetDisplayNameByUser(bl.ReportingLine);

                        txtApprover.Text = bl.Approver;
                        txtMentor.Text = bl.Mentor;

                        chkChangeSalary.Checked = bl.IsChangeSalary;

                        txtGross.Value = bl.GrossSalary;
                        txtBasicSalary.Value = bl.BasisSalary;
                        txtKPI.Value = bl.KPI;
                        txtGenerate.Value = bl.GeneralSusidies;
                        txtLuongCTV.Value = bl.CollaborationSalary;
                        txtGhichu.Text = bl.Note;

                        LoadWRPreviuos();
                    }
                    else
                    {
                        Response.Redirect("HR_ApproveRotationHR_MyApprove.aspx?EmpID=" + HR_ctrEmpHeader.EmployeeId.ToString());
                    }
                }
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }

        public void LoadWRPreviuos()
        {
            try
            {
                HR_WorkingRecordBL bl = new HR_WorkingRecordBL();
                HR_WorkingRecordCollection cl = new HR_WorkingRecordCollection();
                bl = cl.SelectPrevious(HR_ctrEmpHeader.EmployeeId, Convert.ToDateTime(dpkNgayhieuluc.SelectedDate), true, 0);
                if (bl.DTO != null)
                {
                    hdfWRPre.Value = bl.WorkingRecordID.ToString();

                    LS_WorkPlaceBL bl_workplace = new LS_WorkPlaceBL();
                    LS_WorkPlaceCollection cl_workplace = new LS_WorkPlaceCollection();
                    bl_workplace = cl_workplace.SelectByID(bl.LSWorkPlaceID);
                    if (bl_workplace.DTO != null)
                        txtOfficeOld.Text = (SessionManager.LanguageID == "VN" || string.IsNullOrEmpty(bl_workplace.NameEN)) ? bl_workplace.NameVN : bl_workplace.NameEN;

                    //Load cơ cấu tổ chức
                    txtOrgChartOld.Text = cl.SelectOrgChartName(bl.WorkingRecordID);

                    //Load cơ cấu chuyên môn
                    txtProChartOld.Text = cl.SelectProJobNameFull(bl.WorkingRecordID);

                    //Load cơ cấu quản lý
                    txtMgtChartOld.Text = cl.SelectMgtJobNameFull(bl.WorkingRecordID);

                    //Load cấp bậc
                    LS_JobLevelBL bl_joblevel = new LS_JobLevelBL();
                    LS_JobLevelCollection cl_joblevel = new LS_JobLevelCollection();
                    bl_joblevel = cl_joblevel.SelectByID(bl.LSLevelID);
                    if (bl_joblevel.DTO != null)
                        txtLevelOld.Text = (SessionManager.LanguageID == "VN" || bl_joblevel.NameEN == "") ? bl_joblevel.NameVN : bl_joblevel.NameEN;

                    //Load chức vụ
                    LS_JobTitleBL bl_title = new LS_JobTitleBL();
                    LS_JobTitleCollection cl_title = new LS_JobTitleCollection();
                    bl_title = cl_title.SelectByID(bl.JobTitleID);
                    if (bl_title.DTO != null)
                        txtJobTitleOld.Text = (SessionManager.LanguageID == "VN" || bl_title.NameEN == "") ? bl_title.NameVN : bl_title.NameEN;

                    //Load jobrole
                    txtJobRoleOld.Text = cl.SelectJobRoleName(bl.WorkingRecordID);

                    //Load loại nhân viên
                    LS_EmpTypeBL bl_emptype = new LS_EmpTypeBL();
                    LS_EmpTypeCollection cl_emptype = new LS_EmpTypeCollection();
                    bl_emptype = cl_emptype.SelectByID(bl_joblevel.LSEmpTypeID);
                    if (bl_emptype.DTO != null)
                        txtEmpTypeOld.Text = (SessionManager.LanguageID == "VN" || bl_emptype.NameEN == "") ? bl_emptype.NameVN : bl_emptype.NameEN;

                    txtLeaderOld.Text = cl.ReturnLeader(bl.HierarchyId);

                    HR_EmpCVCollection cl_empcv = new HR_EmpCVCollection();
                    txtReporterOld.Text = cl_empcv.GetDisplayNameByUser(bl.ReportingLine);

                    txtApproverOld.Text = bl.Approver;
                    txtMentorOld.Text = bl.Mentor;

                    txtGrossOld.Value = bl.GrossSalary;
                    txtBasicSalaryOld.Value = bl.BasisSalary;
                    txtKPIOld.Value = bl.KPI;
                    txtGenerateOld.Value = bl.GeneralSusidies;
                    txtLuongCTVOld.Value = bl.CollaborationSalary;
                }
            }
            catch (Exception ex)
            {
                AlertMessage message = UserInterfaceExceptionHandler.HandleExcetion(ex);
                MessageBox.Show(message);
            }
        }
        #endregion
    }
}